﻿

var testTime=0
var info="asdf";
function globalLoadfinsh(){
	var canvas = document.getElementById('canvas');
    var context = canvas.getContext('2d');
    canvas.height= screenHeight
    canvas.width= screenWidth
    context.clearRect(0, 0, canvas.width, canvas.height);
    context.fillStyle="#0ff";
    context.fillRect(0,0,canvas.width,canvas.height);
    context.translate(0.5,0.5);

		initCarrier(context);
		initGlobal(context);
	  product1=new Product(context,1000,170,stockSize);


    //动画循环
      (function drawFrame(t){
          window.requestAnimationFrame(drawFrame);
          context.clearRect(0, 0, canvas.width, canvas.height);
          context.fillStyle="#0ff";
          context.fillRect(0,0,canvas.width,canvas.height);
          myDraw(context);

      	//writeCircle(context,stockend,400)
      }());


       function writeCircle(context,x,y) {
         context.save();         //save和restore可以保证样式属性只运用于该段canvas元素
         context.beginPath();    //开始路径
         context.strokeStyle = "#ffffff";       //设置边线的颜色

         context.moveTo(carrierOffsetX, y);
         context.lineTo(stockBgin, y);

         context.stroke();       //绘制边线
         context.restore();
     }


      myReflash("/Daping",function(val){
      	var taskCarrier= val.data.taskCarrier;
      	testTime++
      	if(testTime>66){
      		testTime=0;
      	}
      	info=1200+parseInt(testTime/6)
      	taskCarrier[0].begin=info


      	carrierTaskRun.perClear();
      	 for(let key  in taskCarrier){
      	 	var ss=taskCarrier[key];
        	 	if(ss.taskNo>0){
		        	 	var arr=	graph.baseDistance(ss.begin,ss.end);
		        	 	carrierTaskRun.updateTask(ss.taskNo,ss.begin,arr[arr.length-2])
        	 	}

      	 	}

      	 carrierTaskRun.myClear();
      	//alert(val)
      },function(val){
        	for(var i=0;i<4;i++){
        	   var	crane1=craneList[i]
              crane1.xa=1
              crane1.xGoal=val[i];
              crane1.xv=(crane1.xGoal-crane1.xCurrent)/1.0;//当前速度

              if(i==1){

                info="change to "+ crane1.xGoal
              }
              crane1.xa=0
      		}
      })
	}

function initGlobal(context){
  staticGraph.push(new Word({context:context,x:screenWidth/2-200,y:80,word:"霞浦AB库大屏展示",font:"40px 宋体"}));

         var y=craneYBase(1);
         staticGraph.push(new Line({context:context,x1:craneBegin,y1:y,x2:stockend,y2:y,width:3,strokeStyle :"rgba(255,255,255,1)"}));

         var y=craneYdown(1);
         staticGraph.push(new Line({context:context,x1:craneBegin,y1:y,x2:stockend,y2:y,width:3,strokeStyle :"rgba(255,255,255,1)"}));


   staticGraph.push(new Line({context:context,x1:craneBegin-3*stockSize,y1:craneYBase(2),x2:stockend,y2:craneYBase(2),width:3,strokeStyle :"rgba(255,255,255,1)"}));
   staticGraph.push(new Line({context:context,x1:craneBegin,y1:craneYBase(3),x2:stockend,y2:craneYBase(3),width:3,strokeStyle :"rgba(255,255,255,1)"}));
   staticGraph.push(new Line({context:context,x1:craneBegin,y1:craneYdown(3),x2:stockend,y2:craneYdown(3),width:3,strokeStyle :"rgba(255,255,255,1)"}));
   staticGraph.push(new Line({context:context,x1:craneBegin,y1:craneYBase(4),x2:stockend,y2:craneYBase(4),width:3,strokeStyle :"rgba(255,255,255,1)"}));
   staticGraph.push(new Line({context:context,x1:craneBegin,y1:craneYdown(4),x2:stockend,y2:craneYdown(4),width:3,strokeStyle :"rgba(255,255,255,1)"}));

   ArcList.push(new Arc({context:context,x:craneBegin,y:craneYBase(1)+ArcDiameter/2,r:ArcDiameter/2,Arcstart:Math.PI/2,Arcstop:Math.PI*3/2}));

   ArcList.push(new Arc({context:context,x:craneBegin,y:craneYBase(3)+ArcDiameter/2,r:ArcDiameter/2,Arcstart:Math.PI/2,Arcstop:Math.PI*3/2}));

   ArcList.push(new Arc({context:context,x:craneBegin,y:craneYBase(4)+ArcDiameter/2,r:ArcDiameter/2,Arcstart:Math.PI/2,Arcstop:Math.PI*3/2}));

    craneList.push(new Crane(context,(craneBegin+craneEnd)/2,craneYBase(1),stockSize))

    var crane = new Crane(context,(craneBegin+craneEnd)/2,craneYBase(2),stockSize);
    crane.xCurrent=1;
    crane.xGoal=1
    craneList.push(crane)
    craneList.push(new Crane(context,(craneBegin+craneEnd)/2,craneYBase(3),stockSize))
    craneList.push(new Crane(context,(craneBegin+craneEnd)/2,craneYBase(4),stockSize))


    for(var i=0;i<colsA;i++){
      storestock.push(new Product(context,stockBgin+i*stockSize,craneYBase(1)-1.25*stockSize,stockSize))
      storestock.push(new Product(context,stockBgin+i*stockSize,craneYBase(1)+stockSize*1.25,stockSize))
      storestock.push(new Product(context,stockBgin+i*stockSize,craneYdown(1)-1.25*stockSize,stockSize))
      storestock.push(new Product(context,stockBgin+i*stockSize,craneYdown(1)+stockSize*1.25,stockSize))
    }

    for(var i=0;i<52;i++){
      storestock.push(new Product(context,stockBgin+i*stockSize-3*stockSize,craneYBase(2)-1.25*stockSize,stockSize))
      storestock.push(new Product(context,stockBgin+i*stockSize-3*stockSize,craneYBase(2)+1.25*stockSize,stockSize))
    }
    for(var j=3;j<5;j++){

		  	for(var i=0;i<colsA;i++){
          storestock.push(new Product(context,stockBgin+i*stockSize,craneYBase(j)-1.25*stockSize,stockSize))
          storestock.push(new Product(context,stockBgin+i*stockSize,craneYBase(j)+stockSize*1.25,stockSize))
          storestock.push(new Product(context,stockBgin+i*stockSize,craneYdown(j)-1.25*stockSize,stockSize))
          storestock.push(new Product(context,stockBgin+i*stockSize,craneYdown(j)+stockSize*1.25,stockSize))
        }

    }
}






var xmlHttpReg = null;

    if (window.ActiveXObject) {//如果是IE
        xmlHttpReg = new ActiveXObject("Microsoft.XMLHTTP");
     } else if (window.XMLHttpRequest) {
        xmlHttpReg = new XMLHttpRequest(); //实例化一个xmlHttpReg
     }
function myReflash(url,fun,err){

		this.url="http://127.0.0.1:12138"+url;
		var doResult = function () {
        if (xmlHttpReg.readyState == 4 && xmlHttpReg.status == 200) {//4代表执行完成
	        var res= JSON.parse(xmlHttpReg.responseText)
	        try{
	        	fun(res)
	        	}catch(e){}
		        if(err){
				    		var data=[];
				    		data.push(Math.random())
				    		data.push(Math.random())
				    		data.push(Math.random())
				    		data.push(Math.random())

				    		err(data)
			    	}
	        setTimeout(ajax, 1000);
        }
		    if(xmlHttpReg.readyState == 4 &&(xmlHttpReg.status == 0||xmlHttpReg.status == 404) ){
		    	if(err){
			    		var data=[];
			    		data.push(Math.random())
			    		data.push(1.0-Math.random())
			    		data.push(Math.random())
			    		data.push(Math.random())

			    		err(data)
		    	}
		 			setTimeout(ajax, 1000);
		    }
    }
		var ajax= function(){
			 xmlHttpReg.open("get", this.url, true);
				try{

					xmlHttpReg.send();
					xmlHttpReg.onreadystatechange = doResult; //设置回调函数
				}catch(e){
						if(err){
								err((Math.random()+0.5)*50000)
						}
							setTimeout(ajax, 1000);
				}
	        }

			ajax();
	}




var cranePic = new Image();
    cranePic.src = "png/crane.png";
    var backPic = new Image();
    backPic.src = "png/back.png";
    var productPic = new Image();
    productPic.src = "png/product.png";

    var carrierBackPic = new Image();
    carrierBackPic.src = "png/carrierBack.png";


    screenHeight= window.screen.height
    screenWidth =window.screen.width
    productBegin =170//起始y坐标
    carrierOffsetX=-20;//边距距离
    colsA=49;


    cols=82
    carrierlength = screenWidth/19;//水平传输线长
    stockSize=parseInt((screenWidth-carrierOffsetX*2-carrierlength*4)/colsA)//货位边长


   var allPath=[];
    staticGraph = [];//静态图像
    ArcList=[];
    storestock = [];//货架位置
    //货位总长度 20 大概是俩边各半个传输线宽度
    stockBgin=carrierOffsetX+carrierlength*1.5+stockSize+2//2 是传输线边框宽度
    stockend=stockBgin+stockSize*(colsA);

    craneList=[];
    craneBegin=stockBgin-stockSize-2;
    craneEnd=stockend+stockSize+2
    craneGap=(screenHeight-productBegin)/11;
    craneGap=craneGap>stockSize*3.5?stockSize*3.5:craneGap
    ArcDiameter=88;





      function myDraw(context){

         context.drawImage(backPic,0,0,canvas.width, canvas.height)
         for(var i=0;i<staticGraph.length;i++){
          staticGraph[i].draw();
        }

        for(var i=0;i<ArcList.length;i++){
          ArcList[i].draw();
        }
      	 	for(var i=0;i<storestock.length;i++){
        		storestock[i].draw();
        	}

		     for(var i=0;i<allPath.length;i++){
				     	 var yzj=		allPath[i]
		     			 yzj.draw();
		     }


		     showCrane(context);
		     var arr =  carrierTaskRun.getAllAnthor();
   			for(var i=0;i<arr.length;i++){
   				 var p=new Product(context,68*i,170,stockSize);
   				 p.setAnchor(arr[i])
   				 p.draw();
   			}


      }

      function showCrane(context){

           for(var i=0;i<craneList.length;i++)
           {
            var crane1=craneList[i]
            var goal=crane1.xGoal;
            var x=	crane1.xv/120.0
             if(i!=1){
				     	 var xmm=crane1.xCurrent;

				     	 if(goal>xmm){//目标在右边
				     	 	  crane1.xa=Math.abs(crane1.xa)
					     	 	if(crane1.xv<0){//向左跑
					     	 		crane1.xv+=crane1.xa/10;
					     	 	}
				     	 	}else if(goal<xmm){//目标在左边
				     	 		 crane1.xa=-Math.abs(crane1.xa)
					     	 	if(crane1.xv>0){//向右跑
					     	 		crane1.xv+=crane1.xa/10;
					     	 	}
				     	 	}
                crane1.xCurrent= xmm+x;
                var ArcLen=ArcDiameter*Math.PI/2;
                var lineLen = (craneEnd-craneBegin)*2
                var allArcPercent=ArcLen*1.0/(lineLen+ArcLen)
                var upPercent =(1-allArcPercent)/2;
                var deffy=0;
                var deffX=0

                if(( crane1.xCurrent-crane1.xGoal)*x>0) {
                  crane1.xCurrent=crane1.xGoal
                }
                if( crane1.xCurrent<upPercent){
                  deffX=(upPercent-crane1.xCurrent)*lineLen;
                  crane1.rotateAngle=0
                  product1.rotateAngle=0;
                }else if( crane1.xCurrent>upPercent+allArcPercent) {//下半截
                  deffy = ArcDiameter;
                  deffX=(crane1.xCurrent-upPercent-allArcPercent)*lineLen;
                  crane1.rotateAngle=Math.PI;
                  product1.rotateAngle=Math.PI;
                }else{
                 var ArcPersent= crane1.xCurrent-upPercent;
                 var arc =ArcPersent/allArcPercent*Math.PI;
                 crane1.rotateAngle=arc;
                 product1.rotateAngle=arc;
                 deffX= -Math.sin(arc)*ArcDiameter/2;
                 deffy= ArcDiameter/2-Math.cos(arc)*ArcDiameter/2;
                }
               var x=craneBegin+deffX
               var y= craneYBase(i+1)+deffy
				     	 crane1.setAnchor({x:x,y:y})
                crane1.draw();

              }else{
               if(crane1.xa!=0) {
                continue;
               }
                product1.rotateAngle=0;
                crane1.xCurrent= crane1.xCurrent+x;
                if(( crane1.xCurrent-crane1.xGoal)*x>0) {
                  crane1.xCurrent=crane1.xGoal
                }
                var lineLen = stockend-craneBegin
                var deffX=crane1.xCurrent*lineLen;
                var x=craneBegin+deffX
                var y= craneYBase(i+1)
                crane1.setAnchor({x:parseInt(x),y:parseInt(y)})
                crane1.draw();
              }
              product1.setAnchor(crane1.getAnchor());
              product1.draw();
           }
		    }

        function getYmm(i){
          var y=stockSize*(i-1);
          if(i>2){
            y+=stockSize-20;
          }

          return y;
        }

        function getCraneY(i){
          var all = getYmm(10);
          var me =getYmm(i);
          var allpx=screenHeight-productBegin;
          return productBegin+allpx*me/all;

        }

    	function 	getVY(CraneId){
    	   	return getCraneY(CraneId)-stockSize*0.75-craneGap/2
    	}




    	function 	getUpStockY(CraneId){
    	   	return getCraneY(CraneId)-stockSize*1.25;
    	}
    	function 	getDownStockY(CraneId){
    	   	return getCraneY(CraneId)+stockSize*1.25;
      }



  function 	craneYBase(CraneId){//垛机y
        if(CraneId==1){
          return 170;
        }else if(CraneId==2){
          return 400;
        }else if(CraneId==3){
          return 486;
        }else{
          return 666;
        }

   }

   function 	craneYdown(CraneId){//垛机y
    return craneYBase(CraneId)+ArcDiameter
}

function pathOffset(CraneId){
		if(CraneId<4){
			return 0;
			}else if(CraneId<8){
				return 1;
			}else {
				return 2;
			}
	}






